package cn.yyh.order.controller;

import cn.yyh.basic.exception.MyException;
import cn.yyh.basic.utils.ContextUtil;
import cn.yyh.basic.utils.PageInfo;
import cn.yyh.basic.utils.R;
import cn.yyh.order.domain.AdoptOrder;
import cn.yyh.order.query.AdoptOrderQuery;
import cn.yyh.order.service.IAdoptOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: AdoptOrderController
 * @Package: cn.yyh.order.controller
 * @date 2021/7/8
 */
@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {
    @Autowired
    IAdoptOrderService adoptOrderService;

    @PostMapping("submit")
    public R submit(@RequestBody Map<String,Object> map, HttpServletRequest request) {
        try {
            String payData = adoptOrderService.submit(map,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId());
            return R.oK().setResultObj(payData);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PostMapping("admin")
    public R admin(@RequestBody AdoptOrderQuery query,HttpServletRequest request) {
        try {
            PageInfo<AdoptOrder> list = adoptOrderService.queryByAdmin(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId());
            return R.oK().data("rows", list.getRows()).data("totals", list.getTotals());
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PostMapping("user")
    public R user(@RequestBody AdoptOrderQuery query,HttpServletRequest request) {
        try {
            PageInfo<AdoptOrder> list = adoptOrderService.queryByUser(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId());
            return R.oK().data("rows", list.getRows()).data("totals", list.getTotals());
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping
    public R listAll() {
        try {
            List<AdoptOrder> list = adoptOrderService.listAll();
            return R.oK().data("list", list);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("{id}")
    public R getById(@PathVariable Long id) {
        try {
            AdoptOrder byId = adoptOrderService.getById(id);
            return R.oK().data("adopt", byId);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }
}
